<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
      
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="用户id">
          <a-input placeholder="请输入用户id" v-decorator="['userId', validatorRules.userId ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="订单单号">
          <a-input placeholder="请输入订单单号" v-decorator="['orderNo', validatorRules.orderNo ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="订单金额(总金额)">
          <a-input-number v-decorator="[ 'buyPrice', validatorRules.buyPrice ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="优惠总金额，单位：分">
          <a-input-number v-decorator="[ 'discountPrice', validatorRules.discountPrice ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="物流金额">
          <a-input-number v-decorator="[ 'logisticsPrice', validatorRules.logisticsPrice ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="最终金额，单位：分">
          <a-input-number v-decorator="[ 'presentPrice', validatorRules.presentPrice ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="金额(分)">
          <a-input-number v-decorator="[ 'payAmount', validatorRules.payAmount ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="付款时间">
          <a-date-picker showTime format='YYYY-MM-DD HH:mm:ss' v-decorator="[ 'paymentTime', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="发货时间">
          <a-date-picker showTime format='YYYY-MM-DD HH:mm:ss' v-decorator="[ 'deliveryTime', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="收货时间">
          <a-date-picker showTime format='YYYY-MM-DD HH:mm:ss' v-decorator="[ 'receiverTime', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="成交时间">
          <a-date-picker showTime format='YYYY-MM-DD HH:mm:ss' v-decorator="[ 'closingTime', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否退换货">
          <a-input placeholder="请输入是否退换货" v-decorator="['hasReturnExchange', validatorRules.hasReturnExchange ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="备注">
          <a-input placeholder="请输入备注" v-decorator="['remark', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="状态">
          <a-input placeholder="请输入状态" v-decorator="['status', validatorRules.status ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="删除状态">
          <a-input placeholder="请输入删除状态" v-decorator="['deleted', validatorRules.deleted ]" />
        </a-form-item>
		
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"

  export default {
    name: "OrdersModal",
    data () {
      return {
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        userId:{rules: [{ required: true, message: '请输入用户id!' }]},
        orderNo:{rules: [{ required: true, message: '请输入订单单号!' }]},
        buyPrice:{rules: [{ required: true, message: '请输入订单金额(总金额)!' }]},
        discountPrice:{rules: [{ required: true, message: '请输入优惠总金额，单位：分!' }]},
        logisticsPrice:{rules: [{ required: true, message: '请输入物流金额!' }]},
        presentPrice:{rules: [{ required: true, message: '请输入最终金额，单位：分!' }]},
        payAmount:{rules: [{ required: true, message: '请输入金额(分)!' }]},
        hasReturnExchange:{rules: [{ required: true, message: '请输入是否退换货!' }]},
        status:{rules: [{ required: true, message: '请输入状态' }]},
        deleted:{rules: [{ required: true, message: '请输入删除状态!' }]},
        },
        url: {
          add: "/orders/orders/add",
          edit: "/orders/orders/edit",
        },
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'userId','orderNo','buyPrice','discountPrice','logisticsPrice','presentPrice','payAmount','hasReturnExchange','remark','status','deleted'))
		  //时间格式化
          this.form.setFieldsValue({paymentTime:this.model.paymentTime?moment(this.model.paymentTime):null})
          this.form.setFieldsValue({deliveryTime:this.model.deliveryTime?moment(this.model.deliveryTime):null})
          this.form.setFieldsValue({receiverTime:this.model.receiverTime?moment(this.model.receiverTime):null})
          this.form.setFieldsValue({closingTime:this.model.closingTime?moment(this.model.closingTime):null})
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            formData.paymentTime = formData.paymentTime?formData.paymentTime.format('YYYY-MM-DD HH:mm:ss'):null;
            formData.deliveryTime = formData.deliveryTime?formData.deliveryTime.format('YYYY-MM-DD HH:mm:ss'):null;
            formData.receiverTime = formData.receiverTime?formData.receiverTime.format('YYYY-MM-DD HH:mm:ss'):null;
            formData.closingTime = formData.closingTime?formData.closingTime.format('YYYY-MM-DD HH:mm:ss'):null;
            
            console.log(formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>